package com.lhoverc.service.user.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lhoverc.mapper.LogMapper;
import com.lhoverc.mapper.UserMapper;
import com.lhoverc.pojo.PageBean;
import com.lhoverc.pojo.VO.UserInfoVO;
import com.lhoverc.pojo.dto.LoginUserDto;
import com.lhoverc.pojo.dto.RegisterUserDto;
import com.lhoverc.pojo.entity.UserEntity;
import com.lhoverc.service.user.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: LiCheng
 * @CreateTime: 2025-04-09
 * @Description:
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogMapper logMapper;

    @Override
    public UserEntity login(LoginUserDto loginUserDto) {
        UserEntity user_find = userMapper.getByPhoneAndPassword(loginUserDto);
        if (user_find != null) {
            //todo
            //添加日志,在日志功能添加登录日志
        }
        return user_find;
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.updateIsDelete(id);
    }

    @Override
    public void addUser(RegisterUserDto registerUserDto) {
        UserEntity user = new UserEntity();
        BeanUtils.copyProperties(registerUserDto, user);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setStatus(2);
        userMapper.insertUser(user);
    }

    @Override
    public void updateUser(UserEntity user) {
        userMapper.updateUser(user);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String username, String address, Integer status) {
        //1.设置分页参数
        PageHelper.startPage(page, pageSize);
        //2.执行查询
        List<UserEntity> userList = userMapper.page(username, address, status);
        Page<UserEntity> p = (Page<UserEntity>) userList;
        //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public void updatePassword(String email, String password) {
        userMapper.updatePassword(email, password);
    }

    @Override
    public UserInfoVO getUserInfo(Integer id) {
        UserInfoVO userInfoVO = userMapper.getUserById(id);
        return userInfoVO;
    }

    @Override
    public Integer getUserNum() {
        Integer num = userMapper.getUserNum();
        return num;
    }
}
